#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


#define KEY 24
#define NUM 4
#define OP 3

int calculator( int s1, int s2, char opt ) {
    switch (opt) {
    case '+': return (s1+s2); break;
    case '-': return (s1-s2); break;
    case '*': return (s1*s2); break;
    case '/':
        if ( s2 != 0 && s1 % s2 == 0 ) return (s1/s2);
        else {
            // printf ("\n  %d / %d \n", s1, s2);
            return -99999;
        }
        break;
    default:  
        printf("Err opt\n");
        exit(-1);
        break;
    }
}

int* sgroup ( int* s, int row_of_num, int col ) {
    int *num = (int*)malloc(NUM*row_of_num*sizeof(int));
    int a, b, c, d, i = 0;
    for ( a = 0; a < col; a++ ) {
        for ( b = 0; b < col; b++ ) {
            if ( a == b ) continue;
            for ( c = 0; c < col; c++ ) {
                if ( a == c || b == c ) continue;
                for ( d = 0; d < col; d++ ) {
                    if ( a == d || b == d || c == d ) continue;
                    num[i++] = s[a];
                    num[i++] = s[b];
                    num[i++] = s[c];
                    num[i++] = s[d];
                    // printf("%d  ---  %d  %d  %d  %d \n", i, s[a], s[b], s[c], s[d]);
                }
            }
        }
    }
    return num;
}

char* ogroup ( char* op, int row_of_opt, int col ) {
    char *opt = (char*)malloc((NUM-1)*row_of_opt*sizeof(int));
    int a, b, c, i = 0;
    for ( a = 0; a < 4; a++ ) {
        for ( b = 0; b < 4; b++ ) {
            for ( c = 0; c < 4; c++ ) {
                opt[i++] = op[a];
                opt[i++] = op[b];
                opt[i++] = op[c];
            }
        }
    }
    return opt;
}

int multiple ( int num ) {
    int result = 1;
    while(num) {
        result*=num;
        num--;
    }
    return result;
}

int main () {
    int a = 0, b = 0, i = 0;
    int* s = (int*)malloc( NUM * sizeof(int) );
    char o[4] = {'+', '-', '*', '/'};
    printf("Please input 4 numbers, sepreated by blanks.\n");
    for ( i = 0; i < NUM; i++ ) {
        scanf("%d",&s[i]);
        if ( s[i] > 13 || s[i] < 1 ) {
            printf("Input Err\n");
            exit(1);
        }
    }
    const int row_of_num = multiple(NUM);
    const int row_of_opt = 4*4*4;
    int *num = sgroup(s,row_of_num,NUM);
    char *opt = ogroup(o,row_of_opt,OP);
    for ( a = 0; a < row_of_num*NUM; a+=NUM ) {
        for ( b = 0; b < row_of_opt*OP; b+=OP ) {
            int k1 = calculator(calculator(calculator(num[a],num[a+1],opt[b]),num[a+2],opt[b+1]),num[a+3],opt[b+2] );
            int k2 = calculator(calculator(num[a],num[a+1],opt[b]),calculator(num[a+2],num[a+3],opt[b+2]),opt[b+1] );
            int k3 = calculator(calculator(num[a],calculator(num[a+1],num[a+2],opt[b+1]),opt[b]),num[a+3],opt[b+2] );
            int k4 = calculator(num[a],calculator(calculator(num[a+1],num[a+2],opt[b+1]),num[a+3],opt[b+2]),opt[b] );
            int k5 = calculator(num[a],calculator(num[a+1],calculator(num[a+2],num[a+3],opt[b+2]),opt[b+1]),opt[b] );
            // printf("\n--- %d %c %d %c %d %c %d \n %d %d %d %d %d ---\n",
            // num[a],opt[b],num[a+1],opt[b+1],num[a+2],opt[b+2],num[a+3],
            // k1, k2, k3, k4, k5
            // );
            if ( k1  == KEY ) {
                printf("((%d%c%d)%c%d)%c%d",num[a],opt[b],num[a+1],opt[b+1],num[a+2],opt[b+2],num[a+3]);
                goto out;
            } else if ( k2 == KEY ) {
                printf("(%d%c%d)%c(%d%c%d)",num[a],opt[b],num[a+1],opt[b+1],num[a+2],opt[b+2],num[a+3]);
                goto out;
            } else if ( k3 == KEY ) {
                printf("(%d%c(%d%c%d))%c%d",num[a],opt[b],num[a+1],opt[b+1],num[a+2],opt[b+2],num[a+3]);
                goto out;
            } else if ( k4 == KEY ) {
                printf("%d%c((%d%c%d)%c%d)",num[a],opt[b],num[a+1],opt[b+1],num[a+2],opt[b+2],num[a+3]);
                goto out;
            } else if ( k5 == KEY ) {
                printf("%d%c(%d%c(%d%c%d))",num[a],opt[b],num[a+1],opt[b+1],num[a+2],opt[b+2],num[a+3]);
                goto out;
            }
        }
    }
    printf("Sorry, there's no answer.\n");
    out:
    printf("\n\n");
    free(s);
    free(opt);
    free(num);
    return 0;
}